import React from 'react';
import { Menu, MenuItem, ListItemIcon } from '@mui/material';
import { writeText } from '@tauri-apps/plugin-clipboard-manager';
import { invoke } from '@tauri-apps/api/core';
import Divider from '@mui/material/Divider';
import ArrowRightIcon from '@mui/icons-material/ArrowRight';

function ContextMenu({
  contextMenu,
  onClose,
  download,
  onPauseTask,
  onResumeTask,
  onDeleteTask,
  onCopyLink,
  onSelectAll,
  onSortSelect,
  isAllSelected,
  sortBy,  // 添加这个属性
  onContextMenu,
}) {
  const handlePauseTask = async () => {
    if (download) {
      await onPauseTask(download.id);
    }
  };

  const handleResumeTask = async () => {
    if (download) {
      await onResumeTask(download.id);
    }
  };

  const handleDeleteTask = async () => {
    if (download) {
      await onDeleteTask(download.id);
    }
  };

  const handleCopyLink = async () => {
    if (download) {
      await onCopyLink(download.id);
    }
  };

  const handleSortSelect = (value) => {
    onSortSelect(value);
    onClose();
  };

  const [sortMenuAnchor, setSortMenuAnchor] = React.useState(null);

  const handleSortMenuOpen = (event) => {
    setSortMenuAnchor(event.currentTarget);
  };

  const handleSortMenuClose = () => {
    setSortMenuAnchor(null);
  };

  return (
    <>
      <Menu
        open={Boolean(contextMenu)}
        onClose={onClose}
        anchorReference="anchorPosition"
        anchorPosition={contextMenu ? { top: contextMenu.mouseY, left: contextMenu.mouseX } : undefined}
        onContextMenu={(e) => {
            e.preventDefault();
            onClose;
            onContextMenu(e, contextMenu.downloadId);
          }}
      >
        <MenuItem onClick={onSelectAll}>
          {isAllSelected ? "取消全选" : "全部选中"}
        </MenuItem>
        
        {download && (
          <>
            <Divider variant="middle" component="li" />
            {download.status === 'Downloading' && (
              <MenuItem onClick={handlePauseTask}>暂停任务</MenuItem>
            )}
            {download.status === 'Paused' && (
              <MenuItem onClick={handleResumeTask}>继续任务</MenuItem>
            )}
            <MenuItem onClick={handleDeleteTask}>删除</MenuItem>
            <Divider variant="middle" component="li" />
            <MenuItem onClick={handleSortMenuOpen}>
          显示顺序
            <ArrowRightIcon fontSize="small" />
            </MenuItem>
            <Divider variant="middle" component="li" />
            <MenuItem onClick={handleCopyLink}>复制链接</MenuItem>
          </>
        )}
      </Menu>

      <Menu
        anchorEl={sortMenuAnchor}
        open={Boolean(sortMenuAnchor)}
        onClose={handleSortMenuClose}
        anchorOrigin={{
          vertical: 'top',
          horizontal: 'right',
        }}
        transformOrigin={{
          vertical: 'top',
          horizontal: 'left',
        }}
        onContextMenu={(e) => {
            e.preventDefault();
            handleSortMenuClose;
            onClose;
            onContextMenu(e, contextMenu.downloadId);
          }}
      >
        <MenuItem onClick={() => handleSortSelect('name')}>
          按文件名
        </MenuItem>
        <MenuItem onClick={() => handleSortSelect('size')}>
          按文件大小
        </MenuItem>
        <MenuItem onClick={() => handleSortSelect('progress')}>
          按进度
        </MenuItem>
        <MenuItem onClick={() => handleSortSelect('speed')}>
          按下载速度
        </MenuItem>
        <MenuItem onClick={() => handleSortSelect('remainingTime')}>
          按剩余时间
        </MenuItem>
      </Menu>
    </>
  );
}

export default ContextMenu;